/*
 * Copyright (C) 2009 Show SMS open source project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bydavy.util.showsms;

//~--- non-JDK imports --------------------------------------------------------

import android.app.Activity;
import android.app.AlertDialog;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;

import android.preference.PreferenceManager;

import com.bydavy.android.showsms.R;

/**
 * Class to manage the Features Promotion dialog. It automatically detect if the
 * Features Promotion was yet displayed for the current application version, and display it or not.
 * Must be call on the begin on the "onCreate" of each application Activity.
 *
 * @version        1.0, 09/06/27
 * @author         Davy L.
 */
public class FeaturesPromotion {
    //~--- static fields ------------------------------------------------------

    /** Field description */
    private static String versionName;

    //~--- methods ------------------------------------------------------------

    /**
     * Must be call on the  "onCreate" of each application Activity.
     *     Will display the FeaturesPromotion only if wasn't yeat displayed for this application version.
     *
     * @param activity Caller Activity
     */
    public static void Show(final Activity activity) {
        boolean           yetDisplayed        = false;
        SharedPreferences mySharedPreferences =
            PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());

        // loading enabled status from SharedPreferences
        String version = mySharedPreferences.getString(activity.getString(R.preferences.features_promotions_key), null);

        versionName = Infos.getVersionName(activity.getApplicationContext());

        // if the version name haven't changed, don't display the dialog
        if (version.equals(versionName)) {
            yetDisplayed = true;
        }

        // If not yet displayed for this version or debugging
        if ((!yetDisplayed)|| Log.DEBUG ) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            String                    title   = activity.getString(R.string.app_name) + " " + versionName;

            builder.setTitle(title);
            builder.setMessage(R.string.feature_promot_msg);
            builder.setPositiveButton(R.string.feature_promot_close, new OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    saveDisplayedVersion(activity.getApplicationContext());
                }
            });
            builder.setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    saveDisplayedVersion(activity.getApplicationContext());
                }
            });
            builder.setCancelable(true);
            builder.show();
        }
    }

    /**
     * Action processed when FeaturesPromotion is closed
     *
     * @param context Application context
     */
    private static void saveDisplayedVersion(Context context) {
        SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

        mySharedPreferences.edit().putString(context.getString(R.preferences.features_promotions_key),
                versionName).commit();
    }
}
